<script setup>
import { ref } from 'vue'

const props = defineProps({
    placeholder: {
        type: String,
        default: '输入画面描述，例如：阳光明媚，一个男孩站在大树下面，白色衬衫'
    },
    imageFileUrl: {
        type: String,
        default: ''
    },
})

// 页面参数
const queryParams = ref({
    prompt: '',
    promptObj: {
        // scne: '',
        // primary: '',
        // element: '',
        // color: '',
        // light: '',
        // style: ''
    }
})
// 提交数据
const queryData = ref({})
// 是否显示描述词
const isShowArea = ref(true)
// 是否显示历史记录
const selectShow = ref(false)
// 历史记录列表
const selectList = ref([])




// 跳转页面
function toPage() {
    const data = {
        baseImageUrl: props.imageFileUrl,
    }
    uni.navigateTo({
        url: `/newEditionDraw/textContentProcessing/index?item=${JSON.stringify(data)}`
    })
}


// 设置prompt
function setPrompt(text) {
    queryParams.value.prompt = text
}

// 获取历史记录
function getPromptInfo() {
    formatData()
    return queryParams.value
}


// 保存历史记录
function setLocalSelectList() {
    const isPrompt = selectList.value[selectList.value.length - 1]?.prompt !== queryParams.value.prompt
    if (queryParams.value.prompt && isPrompt) {
        selectList.value.unshift(queryParams.value)
        uni.setStorageSync('vipGenerationBgcHistory', selectList.value)
    }
}

// promptInput事件
function promptInput(e) {
    queryParams.value.prompt = e.detail.value
    queryParams.value.promptObj = {}
}

// 格式化数据
function formatData() {
    if (isShowArea.value) {
        if (!Object.values(queryParams.value.promptObj).length) {
            queryParams.value.promptObj.scne = queryParams.value.prompt
        }
    } else {
        queryParams.value.prompt = Object.values(queryParams.value.promptObj).join('')
    }
}


// 三角点击事件
function sanjiaoClick() {
    isShowArea.value = !isShowArea.value
    if (!isShowArea.value) {
        if (!Object.values(queryParams.value.promptObj).length) {
            queryParams.value.promptObj.scne = queryParams.value.prompt
        }
    } else {
        queryParams.value.prompt = Object.values(queryParams.value.promptObj).join('')
    }
}

// 商品背景历史记录
function tgpHistoryClick() {
    selectShow.value = true
    selectList.value = uni.getStorageSync('vipGenerationBgcHistory') || []
}

// 点击遮罩层
function clickOverlay() {
    selectShow.value = false
}

// 点击选中项
function clickSelectItem(item) {
    queryParams.value = item
    selectShow.value = false
}

// 删除选中项
function clearSelectItem(index) {
    selectList.value = selectList.value.filter((undefined, i) => i !== index)
    uni.setStorageSync('vipGenerationBgcHistory', selectList.value)
}

defineExpose({
    setPrompt,
    getPromptInfo,
    setLocalSelectList,
})
</script>

<template>
    <div class='tgpTextarea'>
        <div class="title_box">
            <div>
                输入描述词
                <span class="required">*</span>
            </div>
            <div class="textarea_title_right">
                <div class="textarea_title_right_item" v-if="isShowArea" @click="tgpHistoryClick">
                    <i class="iconfont ai-icon-lishi"></i>
                    文案记录
                </div>

                <div class="sanjiao_box" @click="sanjiaoClick">
                    <i class="iconfont ai-icon-xiantiaoshangsanjiao sanjiao" :class="{ active: !isShowArea }"></i>
                </div>
            </div>
        </div>
        <div class="textarea_box" v-if="isShowArea">
            <textarea :value="queryParams.prompt" :maxlength="-1" :placeholder="placeholder" @input="promptInput"
                class="teatarea" placeholder-class="placeholderClass"></textarea>

            <div class="select_box">
                <div class="select_overlay" v-if="isShowArea && selectShow" @click="clickOverlay"></div>
                <div class="select_content" :class="{ show: selectShow }">
                    <div class="select_item" v-if="selectList.length" v-for="item, index of selectList" :key="index"
                        @click="clickSelectItem(item)">
                        <div class="text">{{ item.prompt }}</div>
                        <div class="icon_box">
                            <i class="iconfont ai-icon-close" @click.stop="clearSelectItem(index)"></i>
                        </div>
                    </div>
                    <div v-else class="noText">暂无最新数据...</div>
                </div>
            </div>
            <div class="ai_create_btn" @click="toPage" v-if="!selectShow && imageFileUrl">
                文案生成
                <i class="iconfont ai-icon-suiji"></i>
            </div>
        </div>
        <div class="textarea_content" v-else>
            <div class="label_box">场景描述</div>
            <textarea v-model="queryParams.promptObj.scne" :maxlength="-1" class="textarea_content_area"
                placeholder="如：室内、室外、橱窗前、草坪上等"></textarea>

            <div class="label_box">主体描述</div>
            <textarea v-model="queryParams.promptObj.primary" :maxlength="-1" class="textarea_content_area"
                placeholder="如：女生坐在沙发上"></textarea>

            <div class="label_box">元素添加</div>
            <textarea v-model="queryParams.promptObj.element" :maxlength="-1" class="textarea_content_area"
                placeholder="如：沙发上摆着玩具熊"></textarea>

            <div class="label_box">色调</div>
            <textarea v-model="queryParams.promptObj.color" :maxlength="-1" class="textarea_content_area"
                placeholder="如：如：暖色调、冷色调、室外阳光明媚、深棕地板等"></textarea>

            <div class="label_box">光影</div>
            <textarea v-model="queryParams.promptObj.light" :maxlength="-1" class="textarea_content_area"
                placeholder="如：阳光、灯光、柔和光、自然光、左边光源等"></textarea>

            <div class="label_box">风格描述</div>
            <textarea v-model="queryParams.promptObj.style" :maxlength="-1" class="textarea_content_area"
                placeholder="如：欧式、现实、轻奢、现代、中国风、喜庆等"></textarea>
        </div>
    </div>
</template>

<style lang='scss' scoped>
.tgpTextarea {
    .title_box {
        position: relative;
        font-size: 28rpx;
        color: var(--a-one-text-color);
        font-weight: bold;
        padding-left: 30rpx;
        margin: 40rpx 0 18rpx;
        display: flex;
        align-items: center;
        justify-content: space-between;

        &::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translate(0, -50%);
            width: 12rpx;
            height: 18rpx;
            background: var(--a-main-color);
            border-radius: 6rpx;
        }

        .text {
            font-size: 24rpx;
        }

        .required {
            margin-left: 2rpx;
            color: red;
        }
    }

    .label_box {
        font-size: 24rpx;
        color: var(--a-one-text-color);
        margin-top: 20rpx;
    }


    .textarea_title_right {
        font-weight: normal;
        display: flex;
        align-items: center;
        // gap: 33rpx;

        .textarea_title_right_item {
            display: flex;
            align-items: center;
            gap: 5rpx;
            font-size: 24rpx;

            .iconfont {
                font-size: 24rpx;
            }

        }

        .sanjiao_box {
            padding-left: 40rpx;


            .sanjiao {
                display: block;
                font-size: 24rpx;
                transition: all 0.3s ease-in-out;

                &.active {
                    transform: rotate(180deg);
                }
            }
        }
    }


    .textarea_box {
        position: relative;

        .teatarea {
            box-sizing: border-box;
            padding: 20rpx 24rpx 65rpx;
            width: 100%;
            height: 300rpx;
            font-size: 30rpx;
            color: var(--a-one-text-color);
            border-radius: 20rpx;
            background: var(--a-main-box-bg-color);

            ::v-deep.placeholderClass {
                color: #BCBACC !important;
            }
        }



        .select_box {
            .select_overlay {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background-color: rgba(0, 0, 0, 0.5);
                z-index: 9;
            }

            .select_content {
                box-sizing: border-box;
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                height: 0;
                padding: 0 24rpx;

                background-color: var(--a-main-box-bg-color);
                border-radius: 20rpx;
                transition: all 0.3s;
                z-index: 10;
                overflow: scroll;
                opacity: 0;

                &::-webkit-scrollbar {
                    display: none;
                }

                &.show {
                    height: 340rpx;
                    box-shadow: 0 4rpx 8rpx rgba(255, 255, 255, 0.2);
                    opacity: 1;
                }

                .select_item {
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    color: var(--a-one-text-color);
                    font-size: 28rpx;
                    padding: 24rpx 0;
                    border-bottom: 1rpx solid var(--a-one-text-color);

                    &:last-child {
                        margin-bottom: 20rpx;
                    }

                    .text {
                        flex: 1;
                        white-space: nowrap;
                        overflow: hidden;
                        text-overflow: ellipsis;
                    }

                    .icon_box {
                        flex-shrink: 0;
                        width: 100rpx;
                        text-align: right;

                        .iconfont {
                            font-size: 26rpx;
                        }
                    }
                }

                .noText {
                    color: var(--a-one-text-color);
                    font-size: 28rpx;
                    text-align: center;
                    padding: 20rpx 0;
                    border-bottom: 1rpx solid #666;

                }
            }
        }

        .ai_create_btn {
            position: absolute;
            right: 16rpx;
            bottom: 16rpx;
            z-index: 2;
            width: 134rpx;
            height: 43rpx;
            background: #57547B;
            border-radius: 23rpx;
            font-size: 20rpx;
            color: var(--a-one-text-color);
            border: 1rpx solid rgba(255, 255, 255, 0.6);
            display: flex;
            align-items: center;
            justify-content: center;
            line-height: 43rpx;
            gap: 5rpx;

            .iconfont {
                font-size: 24rpx;
            }
        }
    }

    .textarea_content {
        .textarea_content_area {
            box-sizing: border-box;
            padding: 20rpx 24rpx;
            margin-top: 15rpx;
            width: 100%;
            height: 100rpx;
            font-size: 28rpx;
            color: var(--a-one-text-color);
            border-radius: 20rpx;
            background: var(--a-main-box-bg-color);

            &::placeholder {
                color: var(--a-three-text-color);
            }
        }
    }
}
</style>